/*
 * @Description: 消息中心   MessageCenterProvider
 * @Version: 
 * @Autor: zhengzhuang
 * @Date: 2019-11-16 16:13:18
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2020-01-04 09:24:47
 */
import 'package:flutter/material.dart';
import 'package:C20Driver/config/service_api.dart';
import 'package:C20Driver/service/service_method.dart';
import 'package:C20Driver/utils/toast.dart';

class MessageCenterProvider with ChangeNotifier {
  List _notification = []; // 通知列表
  int _notPage = 1; // 通知列表请求页面
  int _notPageSize = 10; // 通知列表获取请求页数

  /// 设置通知列表
  setNotification(data) {
    _notification = data;
    notifyListeners();
  }

  /// 获取消息列表
  _getMessageList() async {
    return await DioUtils()
        .request(ReqMethod.POST, ServiceApi.getMessageList, params: {
      'page': _notPage.toString(),
      'pageSize': _notPageSize.toString(),
    }, success: (res) async {
      List resData = res;
      _notification = resData;
      notifyListeners();
      return res;
    });
  }

  /// 下拉刷新
  _dropDownRefreshNotification() async {
    _notPage = 1;
    await DioUtils()
        .request(ReqMethod.POST, ServiceApi.getMessageList, params: {
      'page': _notPage.toString(),
      'pageSize': _notPageSize.toString(),
    }, success: (res) async {
      List resData = res;
      _notification = resData;
      notifyListeners();
    });
  }

  /// 上拉加载
  _pullUpLoadingNotification() async {
    var _page = _notPage + 1;
    await DioUtils()
        .request(ReqMethod.POST, ServiceApi.getMessageList, params: {
      'page': _page.toString(),
      'pageSize': _notPageSize.toString(),
    }, success: (res) async {
      List resData = res;
      if (resData.length != 0) {
        _notPage++;
        _notification.addAll(res);
      } else {
        ToastX.center('暂无更多数据');
      }
      notifyListeners();
    });
  }

  List get notification => _notification;
  int get notPage => _notPage;
  int get notPageSize => _notPageSize;
  get dropDownRefreshNotification => _dropDownRefreshNotification;
  get pullUpLoadingNotification => _pullUpLoadingNotification;
  get getMessageList => _getMessageList;

  List _message = []; // 消息列表
  int _messagePage = 1; // 通知列表请求页面
  int _messagePageSize = 10; // 通知列表获取请求页数

  /// 设置通知列表
  setMessage(data) {
    _message = data;
    notifyListeners();
  }

  /// 下拉刷新
  _dropDownRefreshMessage() async {
    _messagePage = 1;
    await DioUtils().request(ReqMethod.POST, ServiceApi.getChatList, params: {
      'page': _messagePage.toString(),
      'pageSize': _messagePageSize.toString(),
    }, success: (res) async {
      List resData = res;
      _message = resData;
      notifyListeners();
    });
  }

  /// 上拉加载
  _pullUpLoadingMessage() async {
    var _messagePage = _notPage + 1;
    DioUtils().request(ReqMethod.POST, ServiceApi.getChatList, params: {
      'page': _messagePage.toString(),
      'pageSize': _messagePageSize.toString(),
    }, success: (res) async {
      List resData = res;
      if (resData.length != 0) {
        _messagePage++;
        _message.addAll(res);
      } else {
        ToastX.center('暂无更多数据');
      }
      notifyListeners();
    });
  }

  List get message => _message;
  int get messagePage => _messagePage;
  int get messagePageSize => _messagePageSize;
  get dropDownRefreshMessage => _dropDownRefreshMessage;
  get pullUpLoadingMessage => _pullUpLoadingMessage;
}
